//
//  WenZiViewController.m
//  Emoticon
//
//  Created by ytkjs2 on 2020/3/31.
//  Copyright © 2020 ytk. All rights reserved.
//

#import "WenZiViewController.h"
#import "ListViewCell.h"
#import "DownloadViewController.h"

@interface WenZiViewController ()<UIScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>

@property (strong, nonatomic) NSMutableArray * scrollData;

@property (strong, nonatomic) NSMutableArray * collectionData;

@property (assign, nonatomic) int page;

@end

@implementation WenZiViewController

- (NSMutableArray *)scrollData
{
    if (!_scrollData) {
        _scrollData = [NSMutableArray array];
    }
    return _scrollData;
}

- (NSMutableArray *)collectionData
{
    if (!_collectionData) {
        _collectionData = [NSMutableArray array];
    }
    return _collectionData;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    [self.wenziCollectView registerNib:[UINib nibWithNibName:@"ListViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ListViewCell"];
    
    __weak typeof(self)weakSelf = self;
    
    if (self.picType == 3) {
        [PictureData getRequestWithServiceUrl:WENZI_URL parameters:@{} success:^(id  _Nonnull responseObject) {
            NSArray * arr = [responseObject objectForKey:@"data"];
            NSInteger index = 0;
            for (NSDictionary * dic in arr) {
                index ++;
                MainPicModel * model = [MainPicModel mj_objectWithKeyValues:dic];
                [weakSelf.scrollData addObject:model];
                if (index == arr.count) {
                    [weakSelf loadScrollView];
                }
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
    }
    else
        _scrollHeight.constant = 0;
    
    self.wenziCollectView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.collectionData = [NSMutableArray array];
        NSString * urlStr = @"";
        if (weakSelf.isWenList) {
            urlStr =WENZI_PicList_URL;
        }
        else
        {
            urlStr = Carefully_PicList_URL;
        }
        [PictureData getRequestWithServiceUrl:urlStr parameters:@{@"cate_id":@(weakSelf.cate_id),@"page":@(1)} success:^(id  _Nonnull responseObject) {
            NSArray * arr = [responseObject objectForKey:@"data"];
            for (NSDictionary * dic in arr) {
                PicModel * model = [PicModel mj_objectWithKeyValues:dic];
                [weakSelf.collectionData addObject:model];
            }
            weakSelf.page = 1;
            [weakSelf.wenziCollectView reloadData];
            [weakSelf.wenziCollectView.mj_header endRefreshing];
        } failure:^(NSError * _Nonnull error) {
            [weakSelf.wenziCollectView.mj_header endRefreshing];
        }];
    }];
    
    [self.wenziCollectView.mj_header beginRefreshing];
    
    self.wenziCollectView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        
        NSString * urlStr = @"";
        if (weakSelf.isWenList) {
            urlStr =WENZI_PicList_URL;
        }
        else
        {
            urlStr = Carefully_PicList_URL;
        }
        
        weakSelf.page ++;
        [PictureData getRequestWithServiceUrl:Carefully_PicList_URL parameters:@{@"cate_id":@(weakSelf.cate_id),@"page":@(weakSelf.page)} success:^(id  _Nonnull responseObject) {
            NSArray * arr = [responseObject objectForKey:@"data"];

            for (NSDictionary * dic in arr) {
                PicModel * model = [PicModel mj_objectWithKeyValues:dic];
                [weakSelf.collectionData addObject:model];
            }
            [weakSelf.wenziCollectView reloadData];
            [weakSelf.wenziCollectView.mj_footer endRefreshing];
        } failure:^(NSError * _Nonnull error) {
            [weakSelf.wenziCollectView.mj_footer endRefreshing];
        }];
    }];
    
    [self.wenziCollectView.mj_footer beginRefreshing];
    
    
    
}

-(void)loadScrollView
{
    for (int i = 0; i < _scrollData.count; i ++) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setFrame:CGRectMake(10 + 100 * i, 10, 90, 90)];
        btn.tag = i;
        [btn addTarget:self action:@selector(scrollViewBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        MainPicModel * model = _scrollData[i];
        NSString * imgUrl = [model.image stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [btn sd_setImageWithURL:[NSURL URLWithString:imgUrl] forState:0 placeholderImage:[UIImage imageNamed:@"icon_placeholderImage"]];
        btn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        [[btn imageView] setContentMode:UIViewContentModeScaleAspectFill];
        btn.contentHorizontalAlignment= UIControlContentHorizontalAlignmentFill;
        btn.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
        [btn.layer setMasksToBounds:YES];
        [btn.layer setCornerRadius:4.5];
        [_scroll addSubview:btn];
        
        UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 90, 90)];
        lab.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.3];
        lab.text = model.name;
        lab.textColor = [UIColor whiteColor];
        lab.font = [UIFont systemFontOfSize:15];
        lab.textAlignment = NSTextAlignmentCenter;
        [btn addSubview:lab];
    }
    [_scroll setContentSize:CGSizeMake(10 + _scrollData.count * 100, 0)];
}

-(void)scrollViewBtnAction:(UIButton *)btn
{
    MainPicModel * model = _scrollData[btn.tag];
    WenZiViewController * wenziVC = [[WenZiViewController alloc]initWithNibName:@"WenZiViewController" bundle:nil];
    wenziVC.cate_id = model.cate_id;
    wenziVC.isWenList = YES;
    wenziVC.title = model.name;
    [self.navigationController pushViewController:wenziVC animated:YES];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _collectionData.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ListViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ListViewCell" forIndexPath:indexPath];
    
    PicModel * model = _collectionData[indexPath.item];
    
    NSString * imgUrl = [model.image stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [cell.IMG sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"icon_placeholderImage"]];
    cell.IMG.contentMode = UIViewContentModeScaleAspectFill;
    return cell;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 6.5;//上下行
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 6.5;//行距
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(6.5, 12, 6.4, 12 );//分别为上、左、下、右
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    CGFloat width = (rScreenWidth - 40)/3;
    
    return CGSizeMake(width,width * 2436/1370);//887, 1920
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    PicModel * model = _collectionData[indexPath.item];
    DownloadViewController * downloadVC = [[DownloadViewController alloc]initWithNibName:@"DownloadViewController" bundle:nil];
//    downloadVC.picType = 3;
    downloadVC.isLongPic = YES;
    downloadVC.dataSource = [_collectionData mutableCopy];
    downloadVC.picIndex = indexPath.item;
    [self.navigationController pushViewController:downloadVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
